package com.dq.context.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaUserServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @创建人 gewenzhen
 * @创建时间 2019/8/8
 * @描述 微信的配置 获取微信的配置信息
 */
@Configuration
public class WxMaConfiguration {
    // 此处获取配置的方式可以改成你自己的方式，也可以注解等方式获取配置等。
    private static final String appId = "wx1281f9eefba5a8ab";
    private static final String secret = "82a09af11e42f656d2547dc83042eaa2";

    private static final String appAppId = "wx1281f9eefba5a8ab";
    private static final String token = "";
    private static final String aesKey = "";
    private static final String mch_id = "1557529891";

    private static final String key = "LiYanXiaoChengXu2019102511111111";

    private static WxMaService wxMaService = null;

    private static WxMaUserService wxMaUserService = null;

    private static WxPayService wxPayService = null;

    private static WxPayService wxAppPayService = null;

    public static String getAppId() {
        return appId;
    }

    public static String getKey() {
        return key;
    }

    @Bean
    public Object services() {
        WxMaInMemoryConfig config = new WxMaInMemoryConfig();
        config.setAppid(appId);
        config.setSecret(secret);
        config.setToken(token);
        config.setAesKey(aesKey);

        wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(config);

        wxMaUserService = new WxMaUserServiceImpl(wxMaService);

        return Boolean.TRUE;
    }

    public static WxMaService getWxMaService() {
        return wxMaService;
    }

    public static WxMaUserService getMaUserService() {
        return wxMaUserService;
    }


    @Bean
    public WxPayService wxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(appId);
        payConfig.setMchId(mch_id);
        payConfig.setMchKey(key);
        payConfig.setSubAppId(appId);
        payConfig.setSubMchId("");
        payConfig.setKeyPath("");
        payConfig.setSignType("MD5");

        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);

        wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

    public static WxPayService getWxPayService() {
        return wxPayService;
    }

    @Bean
    public WxPayService wxAppPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(appAppId);
        payConfig.setMchId(mch_id);
        payConfig.setMchKey(key);
        payConfig.setSubAppId(appAppId);
        payConfig.setSubMchId("");
        payConfig.setKeyPath("");
        payConfig.setSignType("MD5");

        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);

        wxAppPayService = new WxPayServiceImpl();
        wxAppPayService.setConfig(payConfig);
        return wxAppPayService;
    }

    public static WxPayService getWxAppPayService() {
        return wxAppPayService;
    }
}
